package com.kl.airticketserver.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kl.airticketserver.dto.Result;
import com.kl.airticketserver.entity.SysDept;
import com.kl.airticketserver.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import java.util.List;

@RestController
@RequestMapping("/api/system/dept")
public class SysDeptController {
    
    @Autowired
    private SysDeptService deptService;
    
    @GetMapping("/list")
    public Result<List<SysDept>> list(@RequestParam(required = false) String name,
                                      @RequestParam(required = false) Integer status) {
        QueryWrapper<SysDept> wrapper = new QueryWrapper<>();
        
        if (StringUtils.hasText(name)) {
            wrapper.like("name", name);
        }
        if (status != null) {
            wrapper.eq("status", status);
        }
        wrapper.orderByAsc("sort");
        
        List<SysDept> result = deptService.list(wrapper);
        return Result.success(result);
    }
    
    @PostMapping
    public Result<Void> save(@RequestBody SysDept dept) {
        deptService.save(dept);
        return Result.success(null);
    }
    
    @PutMapping
    public Result<Void> update(@RequestBody SysDept dept) {
        deptService.updateById(dept);
        return Result.success(null);
    }
    
    @DeleteMapping
    public Result<Void> delete(@RequestBody List<String> ids) {
        deptService.removeByIds(ids);
        return Result.success(null);
    }
}